<?php
/**
 * Created by PhpStorm.
 * User: rhys
 * Date: 2018/3/24
 * Time: 下午9:13
 */

namespace App\Http\Controllers\H5\Shop;
use App\Common\Exceptions\AixinException;
use App\Common\Exceptions\AixinExceptionCode;
use App\Common\Model\ShopAccount;
use App\Common\Service\RecommendService;
use App\Service\PointsService;
use Illuminate\Http\Request;

/**
 * Class RecommendController
 * @package App\Http\Controllers\H5\Shop
 */
class RecommendController
{
    /**
     * @var RecommendService
     */
    private $_recommendService;

    /**
     * RecommendController constructor.
     * @param $_recommendService
     */
    public function __construct(RecommendService $_recommendService)
    {
        $this->_recommendService = $_recommendService;
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws AixinException
     */
    public function userRecommend(Request $request){
        $uid = $request->get('uid');
        $user = ShopAccount::where('uid',$uid)->first();
        if (!$user){
            throw new AixinException(AixinExceptionCode::RESULT_NULL);
        }
        $data = $this->_recommendService->getRecommendInfo($uid);
        if (empty($data)){
            throw new AixinException(AixinExceptionCode::RESULT_NULL);
        }
        return ApiResponse($data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws AixinException
     */
    public function userRecommendList(Request $request){
        $uid = $request->get('uid');
//        $start = $request->get('start');
//        $limit = $request->get('limit');
//        if (!$start && !$limit){
//            throw new AixinException(AixinExceptionCode::MISSING_PARAM);
//        }
        $data = $this->_recommendService->recommendList($uid);
        if (empty($data)){
            throw new AixinException(AixinExceptionCode::RESULT_NULL);
        }
        return ApiResponse($data);
    }

    /**
     * @param Request $request
     * @return array
     * @throws AixinException
     */
    public function userRecommendDetail(Request $request){
        $uid = $request->get('uid');
        $recommend_id = $request->get('recommend_id');
        if (!$recommend_id){
            throw new AixinException(AixinExceptionCode::MISSING_PARAM);
        }
        $user = ShopAccount::where('uid',$recommend_id)->first();
        if (!$user){
            throw new AixinException(AixinExceptionCode::RESULT_NULL);
        }
        $data = $this->_recommendService->getRecommendDetail($uid,$recommend_id);
        if (empty($data)){
            throw new AixinException(AixinExceptionCode::RESULT_NULL);
        }
        return ApiResponse($data);
    }

    /**
     * @param Request $request
     * @return array
     * @throws AixinException
     */
    public function storeRecommend(Request $request){
        $uid = $request->get('uid');
        $recommend_id = $request->get('recommend_id');
        $contact_id = $request->get('contact_id');
        $zone = $request->get('zone');
        if (!$recommend_id && !$contact_id && !$zone ){
            throw new AixinException(AixinExceptionCode::MISSING_PARAM);
        }
        $res = $this->_recommendService->storeRecommend($recommend_id,$contact_id,$zone,$uid);
        if (!$res){
            return ApiResponse([],$this->_recommendService->getError(),1);
        }
        return ApiResponse([]);
    }
}